package com.bzframework.core.auth.manager;

import com.bzframework.core.auth.token.SimpleUserToken;
import com.bzframework.core.auth.token.UserToken;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 本地用户token校验管理器
 */
public class DefaultUserTokenManager implements UserTokenManager {

    private final Map<String, UserToken> tokenStorage = new ConcurrentHashMap<>(256);

    @Override
    public UserToken getUserToken(String token) {
        return tokenStorage.get(token);
    }

    @Override
    public UserToken signIn(String token, String userId, String signInTime, String type) {
        UserToken userToken = UserToken.of(token, userId, signInTime, type);
        tokenStorage.put(token, userToken);
        return userToken;
    }

    @Override
    public void touch(String token) {
        SimpleUserToken userToken = (SimpleUserToken) getUserToken(token);
        userToken.setLastRequestTime(String.valueOf(System.currentTimeMillis()));
        tokenStorage.put(token, userToken);
    }

    @Override
    public void signOut(String token) {
        tokenStorage.remove(token);
    }
}
